Overview

So far, you have built the Menu API and Table booking API in your app.

In this exercise, you will add the feature of registering a new user in your Django project.

Scenario

The objective of this exercise is to be able to add a new user, and modify the existing user's credentials from within the app instead of the admin interface.

You will use the Djoser authentication library. Using the views defined in this library, you can easily implement registration, login and logout actions.

Steps

Step 1

Install the djoser library in the current Django environment with PIP.

Note: You should already have Django REST framework installed.

Step 2

Add the code 'djoser' to the INSTALLED_APPS list in the settings.py file. Ensure that it is placed after the 'rest_framework' app in the list.

Step 3

Add a new section in the settings.py file to set the DJOSER variable.

#add the following line
DJOSER={"USER_ID_FIELD":"username"}

Step 4

Make sure that the TokenAuthentication and SessionAuthentication classes are added to the DEFAULT_AUTHENTICATION_CLASSES section in the REST_FRAMEWORK setting.

Step 5

Enable djoser endpoints by adding the following URL routes in the project's URL patterns.

#add following lines to update urlpatterns list
path('auth/', include('djoser.urls')),
path('auth/', include('djoser.urls.authtoken'))

Step 6

Run the migrations. Start the server and Login to the admin site with the superuser username and password. You should notice the Tokens section on the dashboard.

Step 7

Visit the Browsable API URL User List – Django REST framework to get the list of users already registered.

Step 8

Next you must register new user. To add a new user, use the form and submit a POST request.

Step 9

To login, visit the djoser generated URL http://127.0.0.1:8000/auth/token/login/ .

Enter the username and password to obtain the token.

Step 10

To Logout, enter the URL http://127.0.0.1:8000/auth/token/logout/ with a POST method to logout the user.

Conclusion

In this exercise, you used the djoser library to implement registration, login, and logout features.